package com.faithbj.common.web.controller;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.faithbj.common.model.User;


/**
 * 前台Action类 - 前台基类
 * <p>Copyright: Copyright (c) 2011</p> 
 * 
 * <p>Company: www.faithbj.com</p>
 * 
 * @author 	faithbj
 * @date 	2012-06-16
 * @version 1.0
 */

public class BaseController implements Serializable
{
	
	private static final long serialVersionUID = 4529988972578221098L;
	
	/**
	 * 处理时间的转换器，默认格式是yyyy-MM-dd HH:mm:ss可以被重载
	 * @param binder
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder)
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}

	/**
	 * 获得登陆用户的用户名(无参)
	 * 
	 * @param map
	 * @return
	 */
	protected User getLoginUser()
	{
		Object userObject = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		if (userObject instanceof User)
			return (User) userObject;
		else
			return null;
	}
}
